<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
        <a-dropdown v-if="selectedRowKeys.length > 0">
              <template #overlay>
                <a-menu>
                  <a-menu-item key="1" @click="batchHandleDelete">
                    <Icon icon="ant-design:delete-outlined"></Icon>
                    删除
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button>批量操作
                <Icon icon="mdi:chevron-down"></Icon>
              </a-button>
        </a-dropdown>
        <a-space :bodyStyle="{ padding: '20px' }">
          <a-input v-model:value="scheduleIdValue" style="border-radius: 5px;" placeholder="工单号"/>
          <a-input v-model:value="userNameValue" style="border-radius: 5px;" placeholder="用户名"/>
          <a-input v-model:value="imageIdValue" style="border-radius: 5px;" placeholder="镜像ID"/>
          <a-button type="primary" shape="round" @click="handleSearch">搜索</a-button>
        </a-space>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <!-- 通过,不通过 -->
        <a-space>
          <a-button size="small" type="primary" @click="handleDetail(record)">审批</a-button>
          <a-button size="small" type="danger"  @click="handleDelete(record)">删除</a-button>
        </a-space>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <VmScheduleModal @register="registerModal" @success="handleSuccess"></VmScheduleModal>
  </div>
</template>

<script lang="ts" name="vm_schedule-vmSchedule" setup>
  import {ref} from 'vue';
  import {BasicTable} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import VmScheduleModal from './components/VmScheduleModal.vue'
  import {columns, searchFormSchema} from './VmSchedule.data';
  import {listNoReivew, deleteOne, batchDelete, getImportUrl,getExportUrl} from './VmSchedule.api';

  const scheduleIdValue = ref<string>('');
  const userNameValue = ref<string>('');
  const imageIdValue = ref<string>('');
  const queryParam = ref<any>();
  function handleSearch() {
    queryParam['scheduleUuid'] = scheduleIdValue.value;
    queryParam['userName'] = userNameValue.value;
    queryParam['imageId'] = imageIdValue.value;
    reload();
  }


  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册table数据
  const {tableContext } = useListPage({
      tableProps:{
           title: 'vm_schedule',
           api: listNoReivew,
           columns,
           canResize:false,
           useSearchForm: false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 200,
               fixed:'right'
            },
          beforeFetch: (params) => {
          return Object.assign(params, queryParam);
          },
      },
       exportConfig: {
            name:"vm_schedule",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext

   /**
    * 详情
   */
  function handleDetail(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
   /**
    * 删除事件
    */
  async function handleDelete(record) {
     await deleteOne({id: record.id}, handleSuccess);
   }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
  }
</script>

<style scoped>

</style>